
<template>
    <div class="dise-asso-herb-comp">
         <TitleBox>
            <template #titlecont>
                <span v-lang="{lang: controlSto.language, lgid: -65}">Associated herb</span>
            </template>
        </TitleBox>

        <div class="condition-box">
            <div class="condition-left">
                <SearchDrop class="cond-item">
                    <template #title>
                        <span v-lang="{lang: controlSto.language, lgid: -44}">List sort</span>
                    </template>
                    <template #dropcont>
                        <div class="cond-drop-cont-wrap">
                            <el-radio-group v-model="condObj.sort" @change="toSearch">
                                <div class="cond-radio-wrap">
                                    <el-radio class="cond-radio-item" value="Asc"><span v-lang="{lang: controlSto.language, lgid: -49}"></span></el-radio>
                                    <el-radio class="cond-radio-item" value="Desc"><span v-lang="{lang: controlSto.language, lgid: -50}"></span></el-radio>
                                </div>
                            </el-radio-group>
                        </div>
                    </template>
                </SearchDrop>
            </div>
            <div class="condition-right">
                <div class="ipt-cont">
                        <el-input clearable="true" class="ipt-search" v-model="condObj.searchword" autofocus :placeholder="searchPlace" />
                </div>
                <SearchBtn class="search-item" @click.stop="toSearch"/>

                <TableFilter class="search-item" :column="dataObj.columnList" :filterKey="condObj.filterKey" @onChg="chgTableCol"/>

                <SearchLoad type="diseaseAssHerb" :uiCode="condObj.uiCode" :para="{
                    'content': condObj.searchword,
                    'diseaseId': detailId,
                    'langCode': controlSto.language == 1 ? 'ENG' : 'CHN',
                    'uiTypeCode': condObj.uiCode,
                    'sortType': condObj.sort
                }"/>
            </div>
        </div>


        <div class="cont-box">
            <uiTable
                :config="dataObj.tableConfig"
                :columnConfig="dataObj.columnConfig" 
                :data="dataObj.dataList"
                :loading="isLoading"
                :key="dataObj.searchTablevt">
                <template #[condObj.idField]="{ row }">
                    <div class="jump-box" @click.stop="toClkItem(row, 1)"><span>{{row[condObj.idField]}}</span></div>
                </template>
                <template #[condObj.snameField]="{ row }">
                    <div class="jump-box" @click.stop="toClkItem(row, 2)"><span>{{row[condObj.snameField]}}</span></div>
                </template>
                <template #[condObj.tnameField]="{ row }">
                    <div class="jump-box" @click.stop="toClkItem(row, 3)"><span>{{row[condObj.tnameField]}}</span></div>
                </template>
            </uiTable>
            <div class="page-box" v-if="condObj.total > 10">
                <uiPagination
                    :total="condObj.total" :pageSize="condObj.pageSize" :pageIndex="condObj.pageIndex"
                    @onSizeChange="chgSize" @onCurrentChange="chgCurrnt"/>
            </div>
        </div>
    </div>
</template>

<script setup>
    import { ref, reactive, toRefs, computed, onBeforeMount, watch, inject } from 'vue'
    import TitleBox from '@/components/TitleBox.vue'
    import SearchBtn from '@/components/SearchBtn.vue'
    import TableFilter from '@/components/TableFilter.vue'
    import SearchLoad from '@/components/SearchLoad.vue'
    import uiTable from '@/components/ui-table.vue'
    import uiPagination from '@/components/ui-pagination.vue'
    import SearchDrop from '@/components/SearchDrop.vue'
    import { useRouter } from 'vue-router'

    import { langTxt, getFilterThArr, getFilterLangArr } from '@/utils/util'
    import { getTableThList, getDiseaseAssoHerb } from '@/service/tcm'

    import { controlStore } from '@/stores/control'
    import { debounce } from 'lodash'
    
    const emitter = defineEmits(['toCalc']);
    const reload = inject("reload");
    const router = useRouter();
    const props = defineProps({
        detailId: {
            type: [Number, String],
            default: ''
        }
    });
    const { detailId } = toRefs(props);
    
    let searchDebounce = null;

    const controlSto = controlStore();
    const isLoading = ref(true);
    const condObj = reactive({
        idField: '100716826576557246',
        snameField: '100716826576557247',
        tnameField: '100716826576557250',
		searchword: '',
		filterKey: 'dise2',
        uiCode: 'disease_associated_herbs',
        word: '',
        total: 0,
        pageSize: 10,
        pageIndex: 1,
        sort: 'Asc'
	});

	const dataObj = reactive({
		searchTablevt: 1,
		columnList: [],
		tableConfig: {
			// size: 'small',
			'tooltip-effect': 'dark',
			'highlight-current-row': true,
			stripe: true,
			'header-cell-style': { background: '#BC9F77', color: '#FFF' },
		},
		columnConfig: [],
		dataList: []
	});

    const searchPlace = computed(() => {
		return langTxt(controlSto.language, '-15');
	});

    onBeforeMount(()=> {
        initDebounce();
		getTableTh();
        toGetData();
    });

    const initDebounce = () => {
        searchDebounce = debounce((e) => {
           getDataList();
        }, 400);
    };

    const getTableTh = () => {
        let params = {
            uiCode: condObj.uiCode,
            langCode: 'ENG,CHN'
        };
        getTableThList(params).then((res) => {
            if (res.success) {
                let arr = res.data || [];
                let thArr = getFilterThArr(arr, dataObj.filterKey);
        		let langArr = getFilterLangArr(thArr);
        		controlSto.setLangList(langArr);
        		dataObj.columnList = thArr;
            }
        }).catch(() => {});
        // emitter('toCalc');
    };

    const chgTableCol = (e) => {
		dataObj.columnConfig = e || [];
		dataObj.searchTablevt = new Date().valueOf();

        emitter('toCalc');
    };

    
    const toGetData = () => {
        searchDebounce && searchDebounce();
    };

    const getDataList = () => {
        let params = {
            langCode: controlSto.language == 1 ? 'ENG' : 'CHN',
            diseaseId: detailId.value,
            content: condObj.word,
            uiTypeCode: condObj.uiCode,
			pageNo: condObj.pageIndex || 10,
			pageSize: condObj.pageSize || 1,
            sortType: condObj.sort
        };
        isLoading.value = true;
        getDiseaseAssoHerb(params).then((res) => {
            isLoading.value = false;
            if (res.success && res.data && res.data.records) {
                dataObj.dataList = res.data.records || [];
                condObj.total = res.data.totalCount || 0;
            }
        }).catch(() => {
            isLoading.value = false;
        });

        emitter('toCalc');
    };
        
    const chgSize = (e) => {
        condObj.pageSize = e || 10;
        toGetData();
    };
    const chgCurrnt = (e) => {
        condObj.pageIndex = e || 1;
        toGetData();
    };
    const toSearch = () => {
        condObj.word = condObj.searchword;
        condObj.pageIndex = 1;
        toGetData();
    };
    const toClkItem = (row, type) => {
        if(row[condObj.idField]) {
            const strArr = row[condObj.idField].split('-');
            if (type == 1) {
                router.push({name: 'herbdise', query: { sname: row[condObj.snameField], tname: row[condObj.tnameField]}})
            } else if (type == 2) {
                router.push({name: 'disedetail', query: { id: strArr[0]}})
            } else {
                router.push({name: 'herbdetail', query: { id: strArr[1]}})
            }
        }
    }
    watch(() => controlSto.language, (n, v) => {
        if(n != v) {
            toGetData();
        }
    }, { immediate: true });
    watch(() => detailId, (n, v) => {
        if(n != v) {
            window.scrollTo({top: 0, behavior: 'smooth' });
            reload()
        }
    })

</script>


<style lang="scss" scoped>
    .dise-asso-herb-comp {
        margin-bottom: 32px;
        .condition-box {
            display: flex;
            padding: 24px 0;
            justify-content: space-between;
            .condition-left {
                .cond-item {
                    margin-right: 16px;
                }
            }
            .condition-right {
                .ipt-cont {
                    display: inline-block;
                    padding-right: 16px;
                    .ipt-search {
                        // width: 300px;
                    }
                }
                .search-item {
                    margin-right: 16px;
                }
            }
        }
        .cont-box {
        }
    }
</style>
